﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SkypeTranslator.Utilities
{
	namespace Files
	{
		public class FileProcessor
		{
			public delegate void ProcessFile(string filePath);
			public delegate void ProcessFileCancellable(string filePath, out bool cancel);
			public static void Process(string path, string searchPattern, FileProcessor.ProcessFile proc, bool recursive)
			{
				FileProcessor.Process(path, searchPattern, delegate(string filePath, out bool cancel)
				{
					cancel = false;
					proc(filePath);
				}
				, recursive);
			}
			public static bool Process(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive)
			{
			
				bool result;
				if (Directory.Exists(path))
				{
					FileProcessor.ProcessDirectory(path, searchPattern, proc, recursive, out result);
				}
				else
				{
					if (!File.Exists(path))
					{
						throw new FileNotFoundException(path);
					}
					proc(path, out result);
				}
				return result;
			}
			private static void ProcessDirectory(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive, out bool cancel)
			{
				cancel = false;
				string[] array;
				FileProcessor.GetFiles(path, searchPattern, out array);
				if (array != null)
				{
					string[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						string filePath = array2[i];
						proc(filePath, out cancel);
						if (cancel)
						{
							return;
						}
					}
				}
				string[] array3;
				FileProcessor.GetDirectories(path, searchPattern, proc, recursive, out array3);
			}
			private static void GetFiles(string path, string searchPattern, out string[] fileList)
			{
				fileList = null;
				try
				{
					if (searchPattern == null || searchPattern == string.Empty)
					{
						fileList = Directory.GetFiles(path);
					}
					else
					{
						fileList = Directory.GetFiles(path, searchPattern);
					}
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			private static void GetDirectories(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive, out string[] dirList)
			{
				dirList = null;
				try
				{
					dirList = Directory.GetDirectories(path);
				}
				catch (Exception)
				{
					//Platform.Log(LogLevel.Warn, message);
					throw;
				}
				if (recursive)
				{
					string[] array = dirList;
					for (int i = 0; i < array.Length; i++)
					{
						string path2 = array[i];
						bool flag;
						FileProcessor.ProcessDirectory(path2, searchPattern, proc, recursive, out flag);
						if (flag)
						{
							return;
						}
					}
				}
			}
		}
	}
}
